package com.fingard.dsp.bank.directbank.dlcb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 大连银行余额查询
 * 
 * 参考文档：大连银行银企直联企业对接交易接口(部分)v1.0.2.docx
 * 
 * @author Cruise
 * @date 2018/11/06
 */
public class DLCB01Balance extends DLCB01Base implements IBalance {
	@Override
	public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
		RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
		RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
		retAts.writeStart();
		
		try {
			DLCB01Header tmpBankHeader = new DLCB01Header();
			tmpBankHeader.trncode = "416B600002";
			tmpBankHeader.reqSeqNo = p_req7506.reqHeader.transSeqID;
			
			while (p_req7506.readRecord()) {
				XmlTextWriter xmlWriter = new XmlTextWriter();
	            xmlWriter.autoFormat = false;
	            writeCommonHeader(xmlWriter, tmpBankHeader);
	            
	            xmlWriter.writeStartElement("body");
	            xmlWriter.writeElementString("host_cust_no", bankFront.org);
	            xmlWriter.writeElementString("cur_code", "01");
	            xmlWriter.writeElementString("acno", p_req7506.readedDetail.actNum);
	            xmlWriter.writeElementString("Reserved2", "");
	            xmlWriter.writeEndDocument();
	            
				String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
	            
	            // 模拟银行返回
				/*String[] tmpStrRet = new String[] {"", ""};
				tmpStrRet[1] = "<ap>\r\n" +
						"	<head>\r\n" + 
						"		<tr_code></tr_code>\r\n" + 
						"		<cms_corp_no></cms_corp_no>\r\n" + 
						"		<user_no></user_no>\r\n" + 
						"		<org_code></org_code>\r\n" + 
						"		<serial_no></serial_no>\r\n" + 
						"		<req_no></req_no>\r\n" + 
						"		<tr_acdt></tr_acdt>\r\n" + 
						"		<tr_time></tr_time>\r\n" + 
						"		<succ_flag>0</succ_flag>\r\n" + 
						"		<ret_code>0000</ret_code>\r\n" + 
						"		<ret_info>成功</ret_info>\r\n" + 
						"		<ext_info></ext_info>\r\n" + 
						"		<file_flag></file_flag>\r\n" + 
						"		<total></total>\r\n" + 
						"		<jnl_stat></jnl_stat>\r\n" + 
						"		<reserved></reserved>\r\n" + 
						"	</head>\r\n" + 
						"	<body>\r\n" + 
						"		<balance>100.00</balance>\r\n" + 
						"		<use_balance>100.00</use_balance>\r\n" + 
						"		<freeze_bal>0.00</freeze_bal>\r\n" + 
						"	</body>\r\n" + 
						"</ap>";*/
				
				if (tmpStrRet[0].length() == 0) {
					StringBuilder tmpSbMsg = new StringBuilder();
					XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
					if (tmpBkReader != null) {
						String tmpSuccFlag = tmpBkReader.getFirstText("/ap/head/succ_flag");
						String tmpMsg= tmpBkReader.getFirstText("/ap/head/ret_info");
						if ("0".equals(tmpSuccFlag)) {
							if (tmpBkReader.read("/ap/body")) {
								tmpRetDetail.respCode = "0";
								tmpRetDetail.respInfo = tmpMsg;
								tmpRetDetail.actArea = p_req7506.readedDetail.actArea;
                                tmpRetDetail.actCur = p_req7506.readedDetail.actCur;
                                tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
								tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("balance");
								tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("use_balance");
								tmpRetDetail.frozenBal = tmpBkReader.readedXElement.getSingleText("freeze_bal");
							}
						} else {
							tmpRetDetail.respCode = "-1";
			                tmpRetDetail.respInfo = tmpMsg;
						}
					} else {
						tmpRetDetail.respCode = "-1";
		                tmpRetDetail.respInfo = tmpSbMsg.toString();
					}
				} else {
					tmpRetDetail.respCode = "-1";
	                tmpRetDetail.respInfo = tmpStrRet[1];
				}
			}
			retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
		} catch (Exception ex) {
            ex.printStackTrace();
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(ex.getMessage());
            WriteBankLogLn(ex);
		}

		retAts.writeEnd();
		return retAts;
	}

	@Override
	public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
		return null;
	}
}
